package SGEngine2

import (
	"fmt"
	gl "github.com/gl"
	"github.com/glfw"
)

var (
	RootView *View
)

type View struct {
	Width     int
	Height    int
	Title     string
	ViewScene *Scene
}

func (v View) Run() {

	RootView = &v
	if err := glfw.Init(); err != nil {
		fmt.Println("glfw init err")
		return
	}
	glfw.OpenWindowHint(glfw.OpenGLDebugContext, gl.TRUE)
	if err := glfw.OpenWindow(v.Width, v.Height, 0, 0, 0, 0, 16, 0, glfw.Windowed); err != nil {
		fmt.Println("glfw openwindow err")
		return
	}
	glfw.SetWindowPos(300, 100)
	glfw.SetSwapInterval(1)
	glfw.SetWindowTitle(v.Title)
	glfw.SetMouseButtonCallback(OnMouseClick)
	gl.Init()
	glfw.SetWindowSizeCallback(v.WindowReSize)
	v.ViewScene.Init()

	for glfw.WindowParam(glfw.Opened) == 1 {
		v.ViewScene.Loop()
		glfw.SwapBuffers()
	}
	glfw.Terminate()
	glfw.CloseWindow()
}

func (this *View) WindowReSize(width, height int) {
	this.Width = width
	this.Height = height
	this.ViewScene.Loop()
	glfw.SwapBuffers()
	gl.Viewport(0, 0, width, height)

	EventManager.PushEvent(EVENT_WIN_RESIZE, width, height)
}
